package com.qi.spzx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qi.spzx.manager.mapper.SysRoleMapper;
import com.qi.spzx.manager.mapper.SysRoleUserMapper;
import com.qi.spzx.manager.service.SysRoleService;
import com.qi.spzx.model.dto.system.SysRoleDto;
import com.qi.spzx.model.entity.system.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2024/2/21 19:14
 * @Version 1.0.0
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Override
    public PageInfo<SysRole> findByPage(SysRoleDto sysRoleDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> sysRoleList = sysRoleMapper.findByPage(sysRoleDto) ;
        PageInfo<SysRole> pageInfo = new PageInfo(sysRoleList) ;
        return pageInfo;
    }

    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.saveRole(sysRole);
    }

    @Override
    public void deleteSysRole(Long id) {
        sysRoleMapper.deleteRole(id);
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.updateRole(sysRole);
    }

    /**
     * 查询所有用户的角色
     *
     * @return
     */
    @Override
    public Map<String, Object> findAllRoles(Long userId) {
        // 查询所有的角色数据
        List<SysRole> sysRoleList = sysRoleMapper.findAllRoles();
        // 查询当前登录用户的角色数据
        List<Long> sysRoles = sysRoleUserMapper.findSysUserRoleByUserId(userId);
        //设置返回对象
        Map<String , Object> resultMap = new HashMap<>() ;
        resultMap.put("allRolesList" , sysRoleList) ;
        resultMap.put("sysUserRoles" , sysRoles) ;
        return resultMap;
    }
}
